#!/bin/bash
install_docker() {
    if which docker >/dev/null 2>&1; then
        echo -e "\033[32m检测到 Docker 已安装，跳过安装步骤\033[0m"
        echo "启动 Docker "
        systemctl start docker 2>&1 | tee -a "${CURRENT_DIR}"/install.log
    else
       
        echo -e "\033[31m未检测到 Docker,开始安装 Docker\033[0m"
        echo "下载 Docker 安装包"
        rm -rf docker.tar
        sudo apt-get wget -y
        wget  https://vip.123pan.cn/1812503341/esp32%E7%8E%AF%E5%A2%83/docker.tar
        tar -xvf docker.tar
        if [ $? -eq 0 ]; then
            echo "解压成功"
        else
            echo "解压失败"
            exit 1
        fi
        echo "... 离线安装 docker"
        chmod +x docker/bin/*
        cp docker/bin/* /usr/bin/
        cp docker/service/docker.service /etc/systemd/system/
        chmod 754 /etc/systemd/system/docker.service
        mkdir -p /etc/docker/
        cp docker/conf/daemon.json /etc/docker/daemon.json
        echo "... 启动 docker"
        systemctl enable docker; systemctl daemon-reload; systemctl start docker
    fi
}

configure_docker_mirror() {
    if curl -Is https://www.baidu.com | grep -q "200 OK"; then
        echo -e "\033[31m检测到为国内服务器。\033[0m"
        echo "配置国内服务器加速镜像..."
        # 备份原始的Docker配置文件
        if [ -f /etc/docker/daemon.json ]; then
            sudo cp /etc/docker/daemon.json /etc/docker/daemon.json.bak
            echo "备份原始的Docker配置文件到 /etc/docker/daemon.json.bak"
        fi

# 创建新的Docker配置文件
sudo tee /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": [
    "https://docker.1panelproxy.com",
    "https://docker.1panel.live"
  ]
}
EOF

    # 重新加载Docker配置并重启Docker服务
    echo -e "\033[32m重启docker....\033[0m"
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    else
        echo -e "\033[32m非国内服务器。\033[0m"
    fi
}